define(function(require, exports, module) {
	require("resource/js/easyui/themes/icon.css");
	require("oa/act/approval/form/css/form.css");
	require("resource/js/easyui/easyloader.js");
	require("oa/act/js/loading.js");
	var ajax = require("resource/js/component/ajax.js");
	var ECombobox = require("resource/js/component/easycombobox.js");
	ECombobox.create({
		url: BASEPATH + "/base/authoriza/basecode/getType.htm",
		param: {
			checkeds: "processStatus"
		}
	});
	var pageNo = 1,
		pageSize = 8;

	var initSearchBox = function() {
		using(["combobox"], function() {
			ECombobox.init({
				ele: "#status",
				grouptype: "processStatus",
				onChange: function(newValue, oldValue) {
					var processkey = $('#processkey').combobox('getValue');
					var starttime = $('#starttime').datebox('getValue');
					var endtime = $('#endtime').datebox('getValue');
					dataList(true, 1, 8, newValue, processkey, starttime, endtime);
				}
			});
			ajax.init().success(function(code, msg, data) {
				if (code == 0) {
					var arrasys = [{
						id: '',
						name: '请选择'
					}];
					var retData = arrasys.concat(data);
					$('#processkey').combobox({
						valueField: 'id',
						textField: 'name',
						data: retData,
						onChange: function(newValue, oldValue) {
							var status = $('#status').combobox('getValue');
							var starttime = $('#starttime').datebox('getValue');
							var endtime = $('#endtime').datebox('getValue');
							dataList(true, 1, 8, status, newValue, starttime, endtime);
						}
					});
				}
			}).post(BASEPATH + "/oa/act/generalOperate/getProcessList.htm",{"moudelType":"power_type_3"})
		});
	};

	//遮罩
	function showLoading() {
		$('.jgui-drawer').loading({msg:'操作中',isModel:true});
	}

	function hideLoading() {
		$('.jgui-drawer').loading('hide');
	}
	
	var openDetails = function(row) {
		$(".jgui-drawer-right").removeClass("jgui-drawer-close").addClass("jgui-drawer-open");
		$(".duty_drawer").show();
		$("#jgui-title").text('我的申请（' + row.processname + '）');
		if (row.status == 0) {
			$("#deteleFile").show();
			$("#archiveFile").hide();
			$("#cancelFile").show();
			$("#remindersFile").show();
		} else {
			$("#archiveFile").show();
			$("#cancelFile").hide();
			$("#remindersFile").hide();
			$("#deteleFile").hide(); //只有流程中的流程才能删除
		}
		var urlPath = "";
//		if (row.status == '0' || row.status == '2') {
			urlPath = BASEPATH + "/oa/act/activitiMain/getProcessViewPage.htm?key=" + row.processkey + "&bussId=" + row["businessid"];
//		} else {
//			urlPath = BASEPATH + "/view/oa/act/main/actProcessHistory?url=" + row["path"] + "&bussId=" + row["businessid"];
//		}
		$("#myiframe").attr("src", urlPath);
		//打印
		$("#printFile").unbind("click");
		$("#printFile").on("click",function(){
			window.parent.$(".oa-maskAll").show();
			ajax.init().success(function(code,msg,data){
				window.parent.$(".oa-maskAll").hide();
				if(code==0){
					var param=data.filePath+data.fileId+"."+data.fileExt;
					$.messager.confirm("系统提示",msg,function(r){
						if(r){
							window.open(BASEPATH+"/download/file/"+param);
						}
					});
				}
			}).error(function(code, msg, data) {
				window.parent.$(".oa-maskAll").hide();
				if (msg != "")
					$.messager.show('系统提示', msg);
			}).post(BASEPATH + "/oa/act/print/printprocess.htm", {
				"businessId": row.businessid
			})
		});

		//删除
		$("#deteleFile").unbind("click");
		$("#deteleFile").on("click", function() {
			$.messager.confirm("系统提示", "是否要删除此申请？", function(r) {
				if (r) {
					showLoading();
					ajax.init().success(function(code, msg, data) {
						hideLoading();
						if (code == 0) {
							$(".jgui-drawer-right").find("span.close").click();
							dataList(true, 1, 8);
							$.messager.show({
								title: '系统提示',
								msg: msg,
								timeout: 5000,
								showType: 'slide'
							});
						}
					}).post(BASEPATH + "/oa/act/generalOperate/delete.htm", {
						"businessId": row.businessid
					})
				}
			});

		});

		//撤销
		$("#cancelFile").unbind("click");
		$("#cancelFile").on("click", function() {
			$.messager.confirm("系统提示", "是否要撤销此申请？", function(r) {
				if (r) {
					showLoading();
					ajax.init().success(function(code, msg, data) {
						hideLoading();
						if (code == 0) {
							$(".jgui-drawer-right").find("span.close").click();
							dataList(true, 1, 8);
							$.messager.show({
								title: '系统提示',
								msg: msg,
								timeout: 5000,
								showType: 'slide'
							});
						}
					}).post(BASEPATH + "/oa/act/generalOperate/withdraw.htm", {
						"businessId": row.businessid
					})


				}
			});
		});

		//归档
/*		注释原因：需求放弃此功能
		$("#archiveFile").unbind("click");
		$("#archiveFile").on("click", function() {
			$.messager.confirm("系统提示", "是否要归档此申请？", function(r) {
				if (r) {
					window.parent.$(".oa-maskAll").show();
					ajax.init().success(function(code, msg, data) {
						window.parent.$(".oa-maskAll").hide();
						if (code == 0) {
							$(".jgui-drawer-right").find("span.close").click();
							dataList(true, 1, 8);
						}
						$.messager.show({
							title: '系统提示',
							msg: msg,
							timeout: 5000,
							showType: 'slide'
						});
					}).post(BASEPATH + "/oa/act/generalOperate/filed.htm", {
						"businessId": row.businessid
					})
				}
			});

		});*/

		//催办
		$("#remindersFile").unbind("click");
		$("#remindersFile").on("click", function() {
			ajax.init().success(function(code, msg, data) {
				if (code == 0) {
					$(".jgui-drawer-right").find("span.close").click();
					dataList(true, 1, 8);
				}
				$.messager.show({
					title: '系统提示',
					msg: msg,
					timeout: 5000,
					showType: 'slide'
				});
			}).post(BASEPATH + "/oa/act/actExApproveInfo/doSendMsg.htm", {
				"processkey": row.processkey,
				"checkeds": row.assignee
			})
		});
	}
	$("#findQuery").on("click", function() {
		var status = $('#status').combobox('getValue');
		var processkey = $('#processkey').combobox('getValue');
		var starttime = $('#starttime').datebox('getValue');
		var endtime = $('#endtime').datebox('getValue');
		dataList(true, 1, 8, status, processkey, starttime, endtime);
	});

	var colorNumber = {
		0: "orange",
		1: "green",
		2: "purple",
		3: "red",
		4: "grey"
	}
	
	function parseAbstract(abstractStr){
		var separator = "@VITO@";
		if(abstractStr){
			return abstractStr.split(separator);
		}
	}
	var dataList = function(flag, pageNo, pageSize, status, processkey, starttime, endtime) {
		if (flag) {
			$("#dataList ul").text("");
		}
		$("#query-more a").empty();
		var option = {
				"pageNo":pageNo,
				"pageSize":pageSize,
				"status":status||"",
				"processkey":processkey||"",
				"starttime":starttime||"",
				"endtime":endtime||""
		    }
		ajax.init().success(function(code,msg,data){
			if(data.rows.length>0){
				$("#noData").hide();
				for (var k in data.rows) {
					var abstractStr = data.rows[k]['abstract'] || '';
					if (!data.rows[k].processname) {
						data.rows[k].processname = '流程已被删除';
					}
					$("<li><div><span class=" + colorNumber[data.rows[k].status] + ">" + data.rows[k].statustext + "</span></div>" +
						"<div>" + workBeanchTrim(data.rows[k].processname) + "</div>" +
						"<div><xmp>" + contentNotempty(parseAbstract(abstractStr)&&parseAbstract(abstractStr)[0]) + "</xmp></div>" +
						"<div>" + showEmptyEnd(data.rows[k].assigneename, data.rows[k].starttime) + "</div>" +
						"<div>" + showEmptyEnd(null, data.rows[k].starttime, data.rows[k].endtime) + "</div></li>").appendTo($("#dataList ul")).each(function() {
						$.data(this, {
							data: data.rows[k]
						})
					}).on("click", function() {
						  $(this).addClass("selected").siblings().removeClass("selected");
						  openDetails($.data(this).data);
					});
				}
				$("#dataList").find(".query-more").remove();
				if (data.rows && data.rows.length >= pageSize && data.total > 8) {
					$("<div class='query-more'><a>查看更多</a></div>").appendTo($("#dataList")).on("click", function() {
						pageNo++;
						dataList(false, pageNo, pageSize, status, processkey, starttime, endtime);
					});
				} else {
					if (data.total > 8) {
						$("<div class='query-more'><a style='cursor: default;'>没有更多数据了。</a></div>").appendTo($("#dataList"));
					}
				}
			} else {
				$("#dataList").find(".query-more").remove();
				if(data.total == 0){
					$("#noData").show();
				}else{
					$("<div class='query-more'><a style='cursor: default;'>没有更多数据了！</a></div>").appendTo($("#dataList"));
				}
			}
			$(".jgui-drawer-right").find(".close").click(function() {
				$(".jgui-drawer-right").addClass("jgui-drawer-close").removeClass("jgui-drawer-open");
			});
		}).post(BASEPATH + "/oa/act/actExApproveInfo/queryLaunch.htm", option)
	}

	var workBeanchTrim = function(res) {
		return res || "";
	}

	//内容不能为空，超过30字符显示......
	var contentNotempty =function(content){
		if(content){
			if(content.toString().length>30){
				return content.toString().substring(0,30)+'......';
			}else if(content.toString().length<=30){
				return content.toString().substring(0,30);
			}
		}else{
			return "";
		}
	}

	var showEmptyEnd = function(content, startTime, endTime) {
		if (content) {
			return "下级审批人：" + content;
		} else if (startTime) {
			if (endTime) {
				return "结束时间：" + endTime;
			} else {
				return "发起时间：" + startTime;
			}
		}
	}

	module.exports = {
		openDetail: function(row) {
			return openDetails(row);
		},
		trans: function() {
			using(["parser", "textbox", "numberbox", "dialog",
				"combobox", "messager"
			], function() {
				initSearchBox();
				dataList(true, 1, 8);
				$("#searchform").show();
			});
		}
	}
});